#ifndef ENDIAN_H
#define ENDIAN_H

#include "common.h"
#include <cassert>

#define NET_USHORT(u16)   ((unsigned short)((((unsigned short)(u16)) >> 8) | (((unsigned short)(u16)) << 8)))
#define NET_ULONG(u32)   ((unsigned long)(NET_USHORT((u32) >> 16) | (NET_USHORT(u32 & 0xFFFF) << 16)))
#define NET_SHORT(i16)   ((short)NET_USHORT(i16))
#define NET_LONG(i32)   ((long)(NET_USHORT((i32) >> 16) | (NET_USHORT(i32 & 0xFFFF) << 16)))

static Iu32 endianness = 0xdeadbeef; 
enum endianness { BIG = 1, LITTLE, UNKNOWN };

#define ENDIANNESS ( *(const unsigned char *)&endianness == 0xef ? LITTLE \
                   : *(const unsigned char *)&endianness == 0xde ? BIG \
                   : UNKNOWN)

Iu32 le32toh_(Iu32 u32);

Iu32 be32toh_(Iu32 u32);

Iu16 le16toh_(Iu16 u16);

Iu16 be16toh_(Iu16 u16);

#endif
